﻿using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Models;
using DocumentFormat.OpenXml.ExtendedProperties;
using Newtonsoft.Json;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AJWPFAdmin.Core.HardwareSDKS.EQLED
{
    public enum LEDTextScene
    {
        空闲,
        正在通行,
        已通过,
        报警
    }

    public enum EQLEDCardType
    {
        EQ3002I,
        EQ3002II,
        EQ3002III,
        EQ2008IOrII,
        EQ2010I,
        EQ2008IE,
        EQ2011 = 7,
        EQ2012,
        EQ2008M,
        EQ2013 = 21,
        EQ2023,
        EQ2033,
        EQ2623 = 32,
        EQ2633
    }

    public enum EQLEDCommunicationMode
    {
        串口,
        网络
    }

    public enum EQLEDColorStyle
    {
        单色屏,
        双色屏,
        七彩屏
    }

    public enum EQLEDFontColor
    {
        红色 = 0x00FF,
        黄色 = 0xFFFF,
        绿色 = 0xFF00,

    }

    public class EQLEDControlConfigModel
    {

        public EQLEDSDK.User_Text CreateText(string text, int x = 0, int y = 0,
            int? width = null, int? height = null,
            EQLEDFontColor color = EQLEDFontColor.红色,
            int fontSize = 12)
        {
            width ??= ScreenWidth;
            height ??= ScreenHeight;
            return new EQLEDSDK.User_Text
            {
                chContent = text,
                PartInfo = new EQLEDSDK.User_PartInfo
                {
                    iX = x,
                    iY = y,
                    iWidth = width.GetValueOrDefault(),
                    iHeight = height.GetValueOrDefault()
                },
                BkColor = 0,
                FontInfo = new EQLEDSDK.User_FontSet
                {
                    bFontBold = false,
                    bFontItaic = false,
                    bFontUnderline = false,
                    colorFont = (int)color,
                    iFontSize = fontSize,
                    strFontName = "宋体",
                    iAlignStyle = 0,
                    iVAlignerStyle = 0,
                    iRowSpace = 2,
                },
                MoveSet = new EQLEDSDK.User_MoveSet
                {
                    iActionType = 1,
                    iActionSpeed = 5,
                    iHoldTime = 20,
                    iClearActionType = 1,
                    iClearSpeed = 4,
                    iFrameTime = 20
                }
            };
        }

        /// <summary>
        /// 记录id,用于管理端增删改
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 行高
        /// </summary>
        public int LineHeight { get; set; }

        /// <summary>
        /// 控制卡类型
        /// </summary>
        public EQLEDCardType CardType { get; set; }

        /// <summary>
        /// 控制卡地址, 0 ~ 1023
        /// </summary>
        public int CardAddress { get; set; }

        /// <summary>
        /// 通讯方式
        /// </summary>
        public EQLEDCommunicationMode CommunicationMode { get; set; }

        /// <summary>
        /// 串口号
        /// </summary>
        public int SerialNum { get; set; }

        /// <summary>
        /// 波特率
        /// </summary>
        public int SerialBaud { get; set; }

        /// <summary>
        /// 网络端口
        /// </summary>
        public int NetPort { get; set; } = 5005;

        /// <summary>
        /// IP地址
        /// </summary>
        public string IpAddress { get; set; }

        /// <summary>
        /// 屏幕宽度
        /// </summary>
        public int ScreenWidth { get; set; }

        /// <summary>
        /// 屏幕高度
        /// </summary>
        public int ScreenHeight { get; set; }

        /// <summary>
        /// 屏幕颜色
        /// </summary>
        public EQLEDColorStyle ColorStyle { get; set; }

        /// <summary>
        /// 关联道闸id
        /// </summary>
        public long[] PassagewayIds { get; set; }

        /// <summary>
        /// 关联道闸名称
        /// </summary>
        public string PassagewayNames { get; set; }

        /// <summary>
        /// 节目索引号
        /// </summary>
        public int ProgramId { get; set; }

        /// <summary>
        /// 每行条目配置
        /// </summary>
        public List<EQLEDLineItemConfig> LineConfigs { get; set; }

        public EQLEDControlConfigModel()
        {
            CardType = EQLEDCardType.EQ3002I;
            CardAddress = 1;
            CommunicationMode = EQLEDCommunicationMode.网络;
            ScreenHeight = 32;
            ScreenWidth = 64;
            IpAddress = "127.0.0.1";
            PassagewayIds = Array.Empty<long>();
            LineConfigs = new List<EQLEDLineItemConfig>();
        }

        public override string ToString()
        {
            var ipAddrArray = (IpAddress ?? string.Empty).Split('.');
            return $"[地址：{CardAddress - 1}]\r\nCardType={(int)CardType}\r\nCardAddress={CardAddress - 1}\r\nCommunicationMode={(int)CommunicationMode}\r\nScreemHeight={ScreenHeight}\r\nScreemWidth={ScreenWidth}\r\nSerialBaud={SerialBaud}\r\nSerialNum={SerialNum}\r\nNetPort={NetPort}\r\nIpAddress0={ipAddrArray.ElementAtOrDefault(0)}\r\nIpAddress1={ipAddrArray.ElementAtOrDefault(1)}\r\nIpAddress2={ipAddrArray.ElementAtOrDefault(2)}\r\nIpAddress3={ipAddrArray.ElementAtOrDefault(3)}\r\nColorStyle = {(int)ColorStyle}\r\n";
        }
    }
}
